package com.cfweb.form;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

import com.cfweb.util.FormUtil;

public class UploadAppForm {

	static Logger logger = Logger.getLogger(UploadAppForm.class);

	private File file;			//file of application
	private String appName;		//name of application
	private String domains;		//domains
	private Integer temId;		//name of template
	private String envId;		//DB part, table:cf_environment, field:id
	private String orgName;		//org
	private String spaceName;	//space
	private String instances;	//instance number

	private String memory;		//memory of application
	private String buildPack;	//buildpack
	
	private boolean uploadExample;
	private List<String> services;		//service to bind
	private List<String> patterns;		//Service Log : pattern
	private List<String> patternKeys;	//Service Log : key
	private boolean error;				//Service Log : error
	private boolean exception;			//Service Log : exception
	private boolean oracleMapping;
	private boolean mysqlMapping;
	
	//oracl
	private List<String> appSources;

	/**
	 * get the form of upload-application
	 * @param dataInput
	 * @throws Exception
	 */
	public UploadAppForm(MultipartFormDataInput dataInput) throws Exception {
		Map<String, List<InputPart>> uploadForm = dataInput.getFormDataMap();
		List<InputPart> fileParts = uploadForm.get("uploadFile");
		appName = FormUtil.getFormString(uploadForm.get("appName"));
		domains = FormUtil.getFormString(uploadForm.get("template.domain"));
		
		if (fileParts != null && !fileParts.isEmpty()) {
			InputPart inputPart = fileParts.get(0);
			InputStream inputStream = inputPart.getBody(InputStream.class, null);
			file = new File(appName+"."+domains);
			FormUtil.inputstreamtofile(inputStream, file);
		}
		// here may be wrong as cf_app table in db is changed
		temId = Integer.valueOf(FormUtil.getFormString(uploadForm.get("template.id")));
		envId = FormUtil.getFormString(uploadForm.get("template.envId"));
		buildPack = FormUtil.getFormString(uploadForm.get("env.envName"));
		orgName = FormUtil.getFormString(uploadForm.get("org"));
		spaceName = FormUtil.getFormString(uploadForm.get("space"));
		instances = FormUtil.getFormString(uploadForm.get("template.instance"));
		memory = FormUtil.getFormString(uploadForm.get("template.memory"));
		services = FormUtil.getFormList(uploadForm.get("service"));
		patternKeys = FormUtil.getFormList(uploadForm.get("patternkey"));
		patterns = FormUtil.getFormList(uploadForm.get("pattern"));
		error = FormUtil.getFormBoolean(uploadForm.get("error"));
		exception = FormUtil.getFormBoolean(uploadForm.get("exception"));
		uploadExample = FormUtil.getFormBoolean(uploadForm.get("uploadExample"));
		appSources = FormUtil.getFormList(uploadForm.get("appSource"));
		oracleMapping= FormUtil.getFormBoolean(uploadForm.get("oracleMapping"));
		mysqlMapping= FormUtil.getFormBoolean(uploadForm.get("mysqlMapping"));
	}

	public String getAppName() {
		return appName;
	}
	
	public Integer getTemId() {
		return temId;
	}

	public File getFile() {
		return file;
	}

	public String getDomains() {
		return domains;
	}

	public String getEnvId() {
		return envId;
	}

	public String getOrgName() {
		return orgName;
	}

	public String getSpaceName() {
		return spaceName;
	}

	public String getInstances() {
		return instances;
	}

	public void setInstances(String instances) {
		this.instances = instances;
	}

	public String getMemory() {
		return memory;
	}

	public List<String> getServices() {
		return services;
	}

	public List<String> getPatterns() {
		return patterns;
	}
	
	public List<String> getPatternKeys() {
		return patternKeys;
	}
	
	public boolean getError() {
		return error;
	}
	
	public boolean getException() {
		return exception;
	}

	public String getBuildPack() {
		return buildPack;
	}

	public boolean isUploadExample() {
		return uploadExample;
	}

	public void setUploadExample(boolean uploadExample) {
		this.uploadExample = uploadExample;
	}

	public List<String> getAppSources() {
		return appSources;
	}

	public void setAppSources(List<String> appSources) {
		this.appSources = appSources;
	}

	public boolean isOracleMapping() {
		return oracleMapping;
	}

	public void setOracleMapping(boolean oracleMapping) {
		this.oracleMapping = oracleMapping;
	}

	public boolean isMysqlMapping() {
		return mysqlMapping;
	}

	public void setMysqlMapping(boolean mysqlMapping) {
		this.mysqlMapping = mysqlMapping;
	}
	
}